from sklearn.metrics.pairwise import cosine_similarity
import numpy as np

# 假设bom_matrix是一个历史BOM共现矩阵
bom_matrix = np.array([
    [0, 3, 1, 0, 2],
    [3, 0, 2, 1, 0],
    [1, 2, 0, 1, 3],
    [0, 1, 1, 0, 1],
    [2, 0, 3, 1, 0]
])

# 计算共现矩阵的余弦相似度
similarity_matrix = cosine_similarity(bom_matrix)

# 选定目标组件，假设为索引1的组件
target_component = 1

# 获取目标组件与其他组件的相似性得分
similarity_scores = similarity_matrix[target_component]

# 选取相似性得分最高的若干个组件作为推荐
top_n = 5
recommended_components = similarity_scores.argsort()[-top_n:][::-1]

# 输出推荐结果
print("Recommended components:", recommended_components)
